home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 124 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  76 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: esap@cs.tut.fi (Pulkkinen Esa)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: An STL helper -- and template and type shenanigans
  5. Date: 23 Jan 1996 16:07:45 GMT
  6. Organization: Tampere University of Technology
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4e2dqo$gh@peippo.cs.tut.fi>
  9. References: <30FCDA77.69CB@trilogy.com>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Content-Type: text
  12. X-Nntp-Posting-Host: korppi.cs.tut.fi
  13. Nntp-Posting-User: esap
  14. Content-Length: 1649
  15. Originator: clamage@taumet
  16.  
  17. In article <30FCDA77.69CB@trilogy.com>,
  18. D. Allan Drummond <allan.drummond@trilogy.com> wrote:
  19. [an example and discussion about type dereferencing removed]
  20. >I really want to write something like this (the following code is
  21. >nonsensical but should convey the spirit of the idea):
  22. >
  23. >template<class Container>
  24. >void deep_copy( Container& from, Container& to )
  25. >{
  26. >    to = from; // expand to make sure TO is as big as FROM.
  27. >               // should be cheap, if this is really a list of pointers.
  28. >    Container::const_iterator f( from.begin() );
  29. >    Container::iterator t( to.begin );
  30. >
  31. >    while( (f != from.end()) && (t != to.end()) && *f )
  32. >        *t++ = new (*Container::value_type)( **f++ );
  33. >}
  34. >
  35. >It seems as though there's no REAL reason to have to split it into two
  36. >functions, but the "type dereferencing" is crucial.
  37.  
  38. Well, you can generalize the type deferencing scheme using a partial
  39. specialization:
  40.  
  41. template <class T> class type_dereferencer
  42. {
  43. };
  44.  
  45. template <class T>
  46. class type_dereferencer<T*>
  47. {
  48. public:
  49.   typedef T base_type;
  50. };
  51.  
  52. Of course you would also have to specialize for user-defined
  53. pointer types and STL iterators.
  54.  
  55. Now you can use
  56. type_dereferencer<Container::value_type>::base_type in place of 
  57. (*Container::value_type) in your example.
  58.  
  59. And if typedef templates were allowed, you could use a slightly better
  60. syntax:
  61.  
  62. template <class T>
  63. typedef type_dereferencer<T>::base_type type_dereference;
  64. --
  65.    Esa Pulkkinen                        | C++ programmers do it virtually
  66.    E-Mail:  esap@cs.tut.fi              | everywhere with a class, resulting
  67.    WWW   :  http://www.cs.tut.fi/~esap/ | in multiple inheritance.
  68.  
  69.  
  70.  
  71.  
  72. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  73.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  74.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  75.  
  76.